package com.ltmonitor.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ltmonitor.service.IQueryService;
import com.ltmonitor.service.impl.QueryService;
import com.ltmonitor.util.DateUtil;
import com.ltmonitor.web.util.JsonMessage;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/driverCardRecord")
public class DriverCardRecord extends PaginateAction {

	/**
	 * 更新插拔卡记录中的拍照信息
	 * 1.首先查找最近的没有更新的数据
	 * 2.更新表格
	 * @return
	 */
	@RequestMapping("/updatePicInfo.action")
	public@ResponseBody  JsonMessage updatePicInfo( @RequestBody String jsonStr) {
		JsonMessage result = null;
		int updateNum = -1;
		try {
			Map<String, Object> map1 = JSONObject.fromObject(jsonStr);
			if(queryService == null) {
				result = new JsonMessage(true, "系统没有准备完成");
				return result;
			}
			List fileList = null;
			
			//查找未更新数据
			String queryId = "driverCardRecord.selectUnUpdatedDatas";
			List cmdIdList = queryService.query(queryId, map1);
			if(cmdIdList != null && cmdIdList.size() > 0) {
				Map<String, Object> map = new HashMap<>();
				//开始转换列表
				List listTemp = new ArrayList<>();
				for(Object obj:cmdIdList) {
					Map<String, Object> mapTemp = (Map<String, Object>) obj;
					listTemp.add((int)mapTemp.get("cmdId"));
				}
				
				map.put("cmdIdList", listTemp);
				queryId = "driverCardRecord.selectDriverCardCmdResultByCmdId";
				fileList = queryService.query(queryId, map);
			}
			
			//开始更新数据
			if(fileList != null && fileList.size() > 0) {
				updateNum = 0;
				Map<String, Object> mapTemp = null;
				for(int i=0 ; i<fileList.size() ; i++) {
					mapTemp = (Map<String, Object>) fileList.get(i);
					String fileName = mapTemp.get("fileName").toString();
					if(fileName != "") {
//						int cmdId = (int) mapTemp.get("commandId");
//						String updateDate = mapTemp.get("sendTime").toString();
						queryId = "driverCardRecord.updateDriverCardAttachInfo";
						queryService.query(queryId, mapTemp);
						updateNum  ++;
					}
				}
				result = new JsonMessage(true, updateNum);
			}

			result = new JsonMessage(true, updateNum);
		} catch (Exception e) {
			// TODO: handle exception
			result = new JsonMessage(false, e.getMessage());
			
			e.printStackTrace();
		}
		return result;
	}






	
	
	
	
}
